package com.zzld.core.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.github.abel533.entity.EntityMapper;
import com.github.abel533.entity.Example;
import com.github.abel533.entity.Example.Criteria;
import com.github.abel533.mapper.Mapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

public abstract class BaseServiceImpl<T> implements IBaseService<T> {

	@Autowired
	protected Mapper<T> mapper;

	@Autowired
	private EntityMapper entityMapper;

	public int save(T entity) throws Exception {
		return mapper.insert(entity);
	}

	public int saveSelective(T entity) throws Exception {
		return mapper.insertSelective(entity);
	}

	@Override
	public int updateByPrimaryKey(T entity) throws Exception {
		return mapper.updateByPrimaryKey(entity);
	}

	@Override
	public int updateByPrimaryKeySelective(T entity) throws Exception {
		return mapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public int delete(T entity) throws Exception {
		return mapper.delete(entity);
	}

	@Override
	public int deleteByPrimaryKey(Object key) throws Exception {
		return mapper.deleteByPrimaryKey(key);
	}

	@Override
	public List<T> select(T entity) throws Exception {
		return mapper.select(entity);
	}

	@Override
	public int selectCount(T entity) throws Exception {
		return mapper.selectCount(entity);
	}

	@Override
	public T selectByPrimaryKey(Object key) throws Exception {
		return mapper.selectByPrimaryKey(key);
	}
	
	
	@Override
	public List<T> selectByExample(Object example) throws Exception {
		return mapper.selectByExample(example);
	}

	@Override
	public int selectCountByExample(Object example) throws Exception {
		return mapper.selectCountByExample(example);
	}

	/**
	 * 
	 * @Title: selectPage  分页查询
	 * @Description: TODO
	 * @param pageNum
	 * @param pageSize
	 * @param record
	 * @return
	 * @throws Exception
	 * @see com.zzld.core.service.IBaseService#selectPage(int, int, java.lang.Object)
	 */
	@Override
	public PageInfo<T> selectPage(int pageNum, int pageSize, T record)
			throws Exception {
		PageHelper.startPage(pageNum, pageSize);
		return new PageInfo<T>(mapper.select(record));
	}
	
	
	/**
	 * 
	 * @Title: selectPageByExample 可分页,可排序
	 * @Description: TODO
	 * @param pageNum
	 * @param pageSize
	 * @param example
	 * @return
	 * @throws Exception
	 * @see com.zzld.core.service.IBaseService#selectPageByExample(int, int, java.lang.Object)
	 */
	public PageInfo<T> selectPageByExample(int pageNum, int pageSize,Object example) throws Exception{
		PageHelper.startPage(pageNum, pageSize);
		List<T> list = mapper.selectByExample(example);
		return new PageInfo<T>(list);
	}
}
